/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.miit.elearning.vtutor.check.object;

import java.io.IOException;
import java.util.Map;
import org.apache.commons.net.ftp.FTPFile;
import ru.miit.elearning.vtutor.data.CheckObject;
import ru.miit.elearning.vtutor.ftp.FtpEntry;
import ru.miit.elearning.vtutor.ftp.FTPClientDecorator;

/**
 *
 * @author Cyclone
 */
public class CheckObjectPDSType extends ZOSAbstractCheckObject {

    CheckObjectPDSType(CheckObject co) {
        super(co);
    }

    @Override
    public String getTextContent(FTPClientDecorator ftp) {
        String textContent = null;
        try {
            String[] list = ftp.listPDS(path);
            if (list != null) {

                StringBuilder sb = new StringBuilder();
                for (String name : list) {
                    if (name != null) {
//                        sb.append(name.split("(")[1].split(")")[0]).append("\n");
                        sb.append(name).append("\n");
                    }
                }
                textContent = sb.toString();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return textContent;
    }

    @Override
    protected boolean isObjectListed(FTPClientDecorator ftp) {
        Map<String, FtpEntry> entryMap = ftp.getEntryMap();
        FtpEntry entry = entryMap.get(path.toUpperCase());

        return (entry != null && (entry.getType() == null || entry.getType().equals(FTPFile.DIRECTORY_TYPE)));
    }
}
